package com.wdb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wdb.srv.UserService;

/**
 * @author ld
 * @date 3, 11, 2015
 * @blog http://localhost:8080/WDB/register
 */
public class RegisterServlet extends HttpServlet {
	
	private static final long serialVersionUID = -7715894432269979527L;
	private UserService us;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("RegisterServlet 被销毁！");
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("RegisterServlet 被创建！");
		super.init();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		String name = request.getParameter("username");
		//String pwd = request.getParameter("password");
		//UserService us = new UserService();
		
		PrintWriter out = response.getWriter();
		// out.println(jObject);
		out.print("用户名为!"+name);
		out.flush();
		out.close();
//		try {
//			if(us.doRegister(name, pwd))
//			response.sendRedirect("/home.jsp");
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		// // String nidStr= request.getParameter("nid");
		// // int nid = 0;
		// // nid = TextUtility.String2Int(nidStr);
		// // JSONObject jObject = new JSONObject();
		// // try
		// // {
		// // CommentDAO commentDAO = new CommentDAO();
		// // long commentCount = commentDAO.getSpecifyNewsCommentsCount(nid);
		// // NewsDAO newsDAO = new NewsDAO();
		// // News news = newsDAO.getNews(nid);
		// // JSONObject jObject2 = new JSONObject();
		// // if (!TextUtility.isNull(news.getTitle()))
		// // {
		// // HashMap<String, Object> hashMap = new HashMap<String, Object>();
		// // hashMap.put("nid", news.getNid());
		// // hashMap.put("title", news.getTitle());
		// // hashMap.put("body", news.getBody());
		// // hashMap.put("source", news.getSource());
		// // hashMap.put("replycount", commentCount);
		// // hashMap.put("ptime", news.getPtime());
		// // hashMap.put("imgsrc", news.getImgSrc());
		// // jObject2.put("news", hashMap);
		// // }
		// // jObject.put("ret", 0);
		// // jObject.put("msg", "ok");
		// // jObject.put("data", jObject2);
		// } catch (Exception e)
		// {
		// e.printStackTrace();
		// try
		// {
		// jObject.put("ret", 1);
		// jObject.put("msg", e.getMessage());
		// jObject.put("data", "");
		// } catch (JSONException ex)
		// {
		// ex.printStackTrace();
		// }
		// }
		String name = request.getParameter("username");
		String pwd = request.getParameter("password");
		try {
			us = new UserService();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		try {
			if(us.doLogin(name, pwd).equals("success"))
			response.sendRedirect("/home.jsp");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
